<?php
class CommonTag extends Tag
{
    /**
     * 标签声明
     * @var array
     */
    public $Tag = array(
        'clothescate' => array('block' => 1, 'level' => 4),
        'otherscate' => array('block' => 1, 'level' => 4),
        'navigation' => array('block' => 1, 'level' => 4),
        'goodslist' => array('block' => 1, 'level' => 4),
    		'hotgoods' => array('block' => 1, 'level' => 4),
    );

	/**
     * 男/女装分类菜单标签
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
    public function _clothescate($attr, $content, &$hd)
    {		
		// 排序
		$sort = isset($attr['sort']) ? $attr['sort'] : 'csort ASC';
		$cname = isset($attr['cname']) ? $attr['cname'] : '男装';
		$str = <<<str
<?php
		\$field = K('Category')->where("cname='{$cname}'")->order("{$sort}")->find();
		\$field['url'] = U('List/Index', array('cid'=>\$field['cid']));
		\$field['subMenu'] = K('Category')->where('pid=' . \$field['cid'])->order("{$sort}")->all();
		foreach(\$field['subMenu'] as \$sk => \$sv){
			\$field['subMenu'][\$sk]['subMenu'] = K('Category')->where('pid=' . \$sv['cid'])->order("{$sort}")->all();
			\$field['subMenu'][\$sk]['url'] = U('List/Index', array('cid'=>\$sv['cid']));
			foreach(\$field['subMenu'][\$sk]['subMenu'] as \$ssk => \$ssv){
				\$field['subMenu'][\$sk]['subMenu'][\$ssk]['url'] = U('List/Index', array('cid'=>\$ssv['cid']));
			}
		} 
?>
		{$content}
str;
		return $str;
    }
	
	/**
     * 其他分类菜单标签（不是男装和女装）
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
    public function _otherscate($attr, $content, &$hd)
    {		
		// 排序
		$sort = isset($attr['sort']) ? $attr['sort'] : 'csort ASC';
		$cname = isset($attr['cname']) ? $attr['cname'] : '男装';
		$str = <<<str
<?php
		\$cateData = K('Category')->where("pid=0 AND cname NOT IN ('男装','女装')")->order("{$sort}")->all();
		foreach(\$cateData as \$k=>\$field){
			\$cateData[\$k]['subMenu'] = K('Category')->where('pid=' . \$field['cid'])->order("{$sort}")->all();
		}
?>
		{$content}
str;
		return $str;
    }
	
	/**
     * 导航标签
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
    public function _navigation($attr, $content, &$hd)
    {
    		// 获取记录数
		$rows = isset($attr['rows']) ? $attr['rows'] : 9999;
		
		// 排序
		$sort = isset($attr['sort']) ? $attr['sort'] : 'csort ASC';
		
		$str = <<<str
<?php
		\$topCate = K('Category')->where("pid=0")->limit("{$rows}")->order("{$sort}")->all();
		foreach(\$topCate as \$cfield) :
			\$cfield['url'] = U('List/Index', array('cid'=>\$cfield['cid']));
		
?>
			{$content}

<?php
		endforeach
?>
str;
		return $str;
    }
	
	/**
     * 商品列表标签
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
    public function _goodslist($attr, $content, &$hd){
    		// 获取记录数
		$rows = isset($attr['rows']) ? $attr['rows'] : 9999;
		// 排序
		$sort = isset($attr['sort']) ? $attr['sort'] : 'shelftime DESC';
		// 所属分类
		$cid = isset($attr['topcid']) ? $attr['topcid'] : 0;		
		// 查询条件
		$where = isset($attr['where']) ? $attr['where'] : '1=1';
		if ($cid>0){
			$cidData = K('Category')->getSonCids($cid);
			$where .= " AND category_cid IN (" . implode(',', $cidData) . ")";
		}
		
		$str = <<<str
<?php
		\$goodsData = K('Goods')->where("{$where}")->limit("{$rows}")->order("{$sort}")->all();
		foreach(\$goodsData as \$k=>\$field) :
			\$field['url'] = U('Details/Index', array('gid'=>\$field['gid']));
		
?>
		{$content}
		
<?php
		endforeach
?>
str;
		return $str;
    }
	
	/* 热销商品标签
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
    public function _hotgoods($attr, $content, &$hd){
    		// 获取记录数
		$rows = isset($attr['rows']) ? $attr['rows'] : 9999;
		// 排序
		$sort = isset($attr['sort']) ? $attr['sort'] : 'shelftime DESC';
		
		$str = <<<str
<?php
		\$goodsData = M()->join("__order_list__ ol JOIN __goods__ g ON ol.goods_gid=g.gid")->where("g.indexpic!=''")->field(array('gid','count(*)'=>'c','indexpic'))->group('ol.goods_gid')->order('c desc')->select();
		foreach(\$goodsData as \$k=>\$field) :
			\$field['url'] = U('Details/Index', array('gid'=>\$field['gid']));
		
?>
		{$content}
		
<?php
		endforeach
?>
str;
		return $str;
    }
	
}
